package com.difsonapps.contacttranslator.dao;

import org.hibernate.SessionFactory;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.difsonapps.contacttranslator.dto.EmailDTO;

@Repository
@Transactional
public class EmailDAO {
	@Autowired
	protected SessionFactory sessionFactory;

	@Transactional
	public void insert(EmailDTO emailDTO) {
		sessionFactory.getCurrentSession().save(emailDTO);
	}

	@Transactional
	public void update(EmailDTO emailDTO) {
		sessionFactory.getCurrentSession().update(emailDTO);
	}

	@Transactional
	public EmailDTO get(int id) {
		return (EmailDTO) sessionFactory.getCurrentSession().get(
				EmailDTO.class, id);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public List<EmailDTO> getAll() {
		return (List<EmailDTO>) sessionFactory.getCurrentSession()
				.createQuery("Select t from EmailDTO t").list();
	}

	@Transactional
	public void delete(EmailDTO emailDTO) {
		sessionFactory.getCurrentSession().delete(emailDTO);
	}
}
